#define _CRT_SECURE_NO_WARNINGS 1
#include"queue.h"
#include"sqstack.h"

#define BALLSIZE 27
int check(queue* q)
{
	int i = (q->head + 1) % QUESIZE;
	while (i != q->tail)
	{
		if (q->data[i] > q->data[(i + 1) % QUESIZE])
			return 0;
		i = (i + 1) % QUESIZE;
	}
	return 1;
}


int main()
{
	
	queue* ballqu;
	SQSTACK *st1, *st_5, *st_h;
	int i, balltmp,tmp;
	int time = 0;
	ballqu = qu_create();
	if (!ballqu)
		return -1;
	st1 = st_create();
	
	if (!st1)
		return -1;
	st_5 = st_create();
	if (!st_5)
		return -1;
	st_h = st_create();
	if (!st_h)
		return -1;
	for(i=1;i<=BALLSIZE;i++)
	{
		qu_enqueue(ballqu, &i);
	}
	qu_travel(ballqu);
	while (1)
	{
		qu_dequeue(ballqu, &balltmp);
		time++;
		if (st1->top<3)
		{
			st_push(st1, &balltmp);
		}
		else
		{
			while (!st_isempty(st1))
			{
				st_pop(st1, &tmp);
				qu_enqueue(ballqu, &tmp);
			}
			if (st_5->top < 10)
			{
				st_push(st_5, &balltmp);
			}
			else
			{
				while (!st_isempty(st_5))
				{
					st_pop(st_5, &tmp);
					qu_enqueue(ballqu, &tmp);
				}
				if (st_h->top < 10)
				{
					st_push(st_h, &balltmp);
				}
				else
				{
					while (!st_isempty(st_h))
					{
						st_pop(st_h, &tmp);
						qu_enqueue(ballqu, &tmp);
					}
					qu_enqueue(ballqu, &balltmp);
					if (check(ballqu))
						break;
				}
			}	
		}
	}
	
	printf("time=%d\n", time);
	st_destroy(st1);
	st_destroy(st_5);
	st_destroy(st_h);
	qu_destroy(ballqu);
	return 0;
}